<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head th:replace="common/head::head('','/css/bootstrap/select/bootstrap-select.min.css','/js/laydate/laydate.js,/js/jquery-validate/jquery.validate.min.js,/js/bootstrap/select/bootstrap-select.min.js,/js/bootstrap/select/defaults-zh_CN.min.js')">
</head>
<script th:inline="javascript">
    /*<![CDATA[*/

    var basePath = /*[[${#httpServletRequest.getScheme() + "://" + #httpServletRequest.getServerName() + ":" + #httpServletRequest.getServerPort() + #httpServletRequest.getContextPath()}]]*/ 'basepath';
    function closeModal(){
        parent.closeModal();

    }


    function downloadUserAddTemplate(){
        window.open(basePath+"/lims/user/downloadUserAddTemplate")
    }

    function batchAddUser() {
        var file = $("input[name='file']:eq(0)").val();
        if (file == ""){
            parent.spop({
                template: '请选择导入的文件',
                position  : 'top-center',
                style: 'error',
                autoclose: 3000
            });
            return false;
        }
        parent.loading("正在导入,请稍后.....");
        var formData = new FormData();
        formData.append("file",$("#file")[0].files[0]);
        $.post({
            url: basePath+"/lims/user/batchAddUser",
            data: formData,
            contentType: false,
            processData: false,
            success: function (data) {
                parent.closeLoading();
                if(data.code == 0){
                    parent.spop({
                        template:"导入成功",
                        autoclose:2000,
                        style:'success',
                        position:"top-center"
                    });
                    closeModal();
                    parent.openMainFrame("/user/userList");
                }else {
                    if (data.code == -101){
                        parent.spop({
                            template:data.msg,
                            autoclose:2000,
                            style:'success',
                            position:"top-center"
                        });
                    }
                    if (data.code = -1){
                       var errorMsg = "<table><tr style='color: #985f0d'><td>错误信息</td><td><span style='color: red'>行数</span><span style='color: green'>(内容)</span></td></td></tr>";
                       if (data.data.userNameEmptyRow){
                           errorMsg += ("<tr><td>用户名未填写:</td><td style='color: red'>"+data.data.userNameEmptyRow+"</td></tr>");
                       }
                       if (data.data.userNameRepeatedRow){
                           errorMsg += ("<tr><td>重复用户名:</td><td style='color: red'>"+data.data.userNameRepeatedRow+"</td></tr>");
                       }
                       if (data.data.signedUserNames){
                           errorMsg += ("<tr><td>已注册用户名:</td><td style='color: green'>"+data.data.signedUserNames+"</td></tr>");
                       }
                       if (data.data.orgIdEmptyList){
                           errorMsg += ("<tr><td>组织编码未填写:</td><td style='color: red'>"+data.data.orgIdEmptyList+"</td></tr>");
                       }
                       if (data.data.unAvailableOrgRow){
                           errorMsg += ("<tr><td>组织编码不正确:</td><td style='color: red'>"+data.data.unAvailableOrgRow+"</td></tr>");
                       }
                       if (data.data.statusEmptydRow){
                           errorMsg += ("<tr><td>是否启用未填写:</td><td style='color: red'>"+data.data.statusEmptydRow+"</td></tr>");
                       }
                       errorMsg += "</table>";
                       $("#errorMsg").html(errorMsg);
                       $("#errorMsg").attr("style","");
                       parent.$("#modalIframe").attr("height","360px");
                    }
                    parent.spop({
                        template:data.msg,
                        autoclose:2000,
                        style:'error',
                        position:"top-center"
                    });
                }
            },

            error: function (data) {
                parent.closeLoading();
                spop({
                    template:"上传失败",
                    autoclose:2000,
                    style:'error',
                    position:"center-center"
                });
            }

        });

    }
    
    /*]]>*/
</script>



<body>
    <div class="container" style="margin-top: 2%;">
        <div class="card secondary text-white">
            <button type="button" th:onclick="'javascript:downloadUserAddTemplate()'" class="btn btn-success">下载模板</button>

        </div>
        <div style="margin-top: 2%">
            <input type="file" name="file" th:id="file" accept=".xlsx" />
        </div>
        <div class="card">
            <div class="card-body" th:id="errorMsg" style="display: none"></div>
        </div>
        <div th:align="right" style="margin-top: 2%;">
            <button type="button" th:onclick="'javascript:batchAddUser()'" th:name="addUserBtn"  class="btn btn-primary">导入</button>
            <button type="button" th:onclick="'javascript:closeModal()'" class="btn btn-secondary">关闭</button>
        </div>
    </div>
</body>
</html>